import { request } from '../utils/request.js'
import store from '../store'
import { Toast } from '../utils/tools.js'
import cookie from '@/utils/auth.js'
import { baseURL, appType, fileUrl } from '@/common/baseconfig.js'
import { UserInfo } from "./Users.js"

//基本配置
export async function GetConfig() {
	try {
		let res = await request({
			url: '/Ctrls/GetConfig',
			method: "post",
			mask: false,
		})
		if (res) {
			let a = {};
			res.forEach(item => {
				a[item.idx] = item.val
			})
			store.commit('general/SET_CONFIG', a)
		}
	} catch (e) {
		console.log(e)
		return []
	}
}


// 获取地区配置缓存
export async function distData(params) {
	try {
		let res = await request({
			url: '/Ctrls/distData',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

//枚举
export async function GetEnum() {
	try {
		let { success, data } = await request({
			url: '/Ctrls/GetEnum',
			method: "post",
			mask: false,
		})
		if (success) {
			store.commit('general/SET_ENUM', data)
		}
	} catch (e) {
		console.log(e)
		return []
	}
}

export async function GetSysCoin() {
	try {
		let res = await request({
			url: '/Ctrls/coinlist',
			method: "post",
			mask: false,
		})
		if (res) {
			let a = {};
			res.forEach(item => {
				a[item.coinid] = item
			})
			store.commit('general/SET_COINCONFIG', a)
		}
	} catch (e) {
		console.log(e)
		return []
	}
}


//上传
export async function Upload(file) {
	try {
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: fileUrl + '/Ctrls/Upload',
				header: {
					'Token': cookie.get('Token'),
					'Lang': store.state.setting.localeIdx,
				},
				filePath: file,
				name: 'upfile',
				success({ data }) {
					if (data) {
						data = JSON.parse(data)
						if (!data.success) Toast(data.msg)
						resolve(data)
					}
				},
				fail: (err) => {
					console.log('err', err)
					reject(err);
				}
			})
			uploadTask.onProgressUpdate(function(res) {
				console.log('res===>', res)
			})
		})
	} catch (e) {
		console.log(e)
		return {}
	}
}


//升级
export async function GetAppVersion(params) {
	try {
		//默认新旧版为打包时设置的参数   有paramss则覆盖掉
		// const params = {
		// 	apptype: appType,
		// 	...paramss
		// }
		let res = await request({
			url: '/Ctrls/GetAppVersion',
			method: "post",
			mask: false,
			params
		})
		if (res.success) {
			store.commit('general/SET_UPINFO', res.data)
		}
		return res;
	} catch (e) {
		console.log(e)
		return false
	}
}

// 获取验证码
export async function SendCode_Phone(params) {
	try {
		let { success, msg } = await request({
			url: '/Ctrls/SendCode_Phone',
			method: "post",
			params
		})
		Toast(msg)
		return success
	} catch (e) {
		return []
	}
}

export async function getIndex() {
	try {
		let res = await request({
			url: '/index',
			method: "post",
		})
		return res
	} catch (e) {
		return []
	}
}

export async function SendCode_Mail(params) {
	try {
		let { success, msg } = await request({
			url: '/Ctrls/SendCode_Mail',
			method: "post",
			params
		})
		Toast(msg)
		return success
	} catch (e) {
		return []
	}
}


//app下载地址
export async function GetAllApkVersion() {
	try {
		let { data, success } = await request({
			url: "/Ctrls/GetAllApkVersion",
			method: "post",
		});
		return { data, success };
	} catch (e) {
		return false;
	}
}
export async function gettime() {
	try {
		let res = await request({
			url: "/Ctrls/gettime",
			method: "post",
		});
		if(res.success) store.commit('general/set_timespan', res.data['timespan'])  // 设置时间戳
		return res;
	} catch (e) {
		return false;
	}
}


export async function NumberVerifyCode(params) {
	try {
		let { success, msg } = await request({
			url: '/NumberVerifyCode',
			method: "pet",
			params
		})
		Toast(msg)
		return success
	} catch (e) {
		return []
	}
}